<?php

namespace app\install\service\install\observer;

use app\common\util\base\BaseObserver;
use app\common\util\base\BaseServer;
use sffi\recovery\Factory;

class LoadingMysqlObserver extends BaseObserver
{
    protected string $file;
    public function checkFileExist()
    {
        $this->file = root_path().'mysql/vovoqi_plugs.sql';
        if (!file_exists($this->file)){
            throw new \Exception('数据库配置文件无效。');
        }
        return $this;
    }

    public function handle(BaseServer $server = null)
    {
        $dbConfig = $server->getInputData();
        $dbConfig['safe'] = false;
        try {
            $res = Factory::parseMysql($this->file, $dbConfig);
            $res->exe();
        }catch (\Exception $exception) {
            print_r(sprintf("数据库处理失败:%s[%s]%s", $exception->getFile(), $exception->getLine() ,$exception->getMessage()));die;
        }
    }
}